package com.ce.pms.system.custservice.service.impl;

import java.util.Collections;
import java.util.List;

import com.ce.common.util.BlankUtil;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.dao.ICustEventKindDao;
import com.ce.pms.system.custservice.dao.ICustEventNatureDao;
import com.ce.pms.system.custservice.model.CustEventKindDO;
import com.ce.pms.system.custservice.model.CustEventNatureDO;
import com.ce.pms.system.custservice.service.ICustEventKindService;
import com.ce.pms.system.custservice.vo.CustEventKindVO;
import com.sitechasia.webx.core.support.Page;

/**
 * 描述：事件类型service实现
 * @author 王浩羽
 *
 */
public class CustEventKindServiceImpl  extends AbstractCustomerEventServiceImpl<CustEventKindVO, CustEventKindDO> implements ICustEventKindService
{

	private ICustEventKindDao custEventKindDao;
	private ICustEventNatureDao custEventNatureDao;
	
	public boolean addCustEventKind(CustEventKindVO eventKindVO)
	{
		if(!BlankUtil.isBlank(eventKindVO) && !BlankUtil.isBlank(eventKindVO.getName()))
		{
			if(isNameExisit(eventKindVO.getName())){
				throw new BusinessException("custservice:system_name_exist_exception",BusinessException.WARNING);
			}
			CustEventKindDO eventKindDO = assembleV2D(eventKindVO,null);
			CustEventNatureDO custEventNatureDO = custEventNatureDao.findById(eventKindVO.getEventNatureId());
			eventKindDO.setCustEventNature(custEventNatureDO);
			eventKindDO.setSysInfo(createInfo(eventKindVO.getCreator()));
			return custEventKindDao.saveObject(eventKindDO) != null;
		}
		return false;
	}
	
	protected boolean isNameExisit(String name){
		return custEventKindDao.findUniqueByName(name)!=null;
	}

	public boolean deleteCustEventKindById(Long eventKindId)
	{
		if(eventKindId==null)
		{
			return false;
		}
		try
		{
			custEventKindDao.deleteObjectById(eventKindId);
			return true;
		}
		catch(RuntimeException e)
		{	//如果发生异常，则认为是已经被引用，那么重新抛出异常
			throw new BusinessException("custservice:system_delete_inused_exception",BusinessException.WARNING);
		}
	}
	

	public boolean editCustEventKind(CustEventKindVO eventKindVO)
	{
		if(!BlankUtil.isBlank(eventKindVO) && !BlankUtil.isBlank(eventKindVO.getKeyword()) && !BlankUtil.isBlank(eventKindVO.getName()))
		{
			CustEventKindDO eventKindDO = custEventKindDao.findById(eventKindVO.getKeyword());
			if(eventKindDO==null)
			{
				return false;
			}
			eventKindDO.setRemark(eventKindVO.getRemark());
			eventKindDO.setSequence(eventKindVO.getSequence());
			eventKindDO.setName(eventKindVO.getName());
			CustEventNatureDO custEventNatureDO = custEventNatureDao.findById(eventKindVO.getEventNatureId());
			eventKindDO.setCustEventNature(custEventNatureDO);
			modifyInfo(eventKindDO.getSysInfo(), eventKindVO.getModifier());
			return custEventKindDao.updateObject(eventKindDO) != null;
		}
		return false;
	}


	@SuppressWarnings("unchecked")
	public List<CustEventKindVO> queryCustEventKind(Long eventNatureId)
	{
		if(eventNatureId == null)
		{
			return Collections.EMPTY_LIST;
		}
		List<CustEventKindDO> eventKindDOList = custEventKindDao.queryCustEventKind(eventNatureId);
		sortList(eventKindDOList);
		return assembleDOList2VOList(eventKindDOList);
	}
	
	@Override
	protected int compareDO(CustEventKindDO o1, CustEventKindDO o2)
	{
		int s1 = Integer.MAX_VALUE;
		int s2 = Integer.MAX_VALUE;
		if(o1.getSequence() != null)
		{
			s1 = o1.getSequence();
		}
		if(o2.getSequence() != null)
		{
			s2 = o2.getSequence();
		}
		return s1 - s2;
	}

	protected CustEventKindVO assembleD2V(CustEventKindDO kindDO, CustEventKindVO kindVO){
		CustEventKindVO resultVO = super.assembleD2V(kindDO, kindVO);
		CustEventNatureDO eventNatureDO = kindDO.getCustEventNature();
		if(!BlankUtil.isBlank(eventNatureDO))
		{
			resultVO.setEventNatureId(eventNatureDO.getKeyword());
			resultVO.setEventNatureName(eventNatureDO.getName());
		}
		return resultVO;
	}

	public Page queryCustEventKindListByPage(SearchVO searchVO)
	{
		return queryVOListByPage(searchVO);
	}

	public void setCustEventKindDao(ICustEventKindDao custEventKindDao)
	{
		this.custEventKindDao = custEventKindDao;
	}

	public void setCustEventNatureDao(ICustEventNatureDao custEventNatureDao)
	{
		this.custEventNatureDao = custEventNatureDao;
	}
	@Override
	protected CustEventKindDO findObjectDOById(Long id)
	{
		return custEventKindDao.findById(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Page queryDOListByPage(SearchVO searchVO)
	{
		Page page = custEventKindDao.queryByPage(searchVO);
		List<CustEventKindDO> list = page.getResult();
		sortList(list);
		page.setResult(list);
		return page;
	}
	
	@Override
	protected String getNameExisitExceptionInoKey()
	{
		return "custservice:system_name_exist_exception";
	}
}
